package module.scene.model
{
	import code.consts.ConfigConst;
	
	import com.adobe.serialization.json.JSON;
	import com.darwin.darwinDll.core.Config;
	import com.darwin.darwinIsometric.core.isoConst.IsoType;
	import com.darwin.darwinMVC.core.CmdNameList;
	import com.darwin.darwinMVC.mvc.Command;
	
	import flash.geom.Point;
	import flash.utils.Dictionary;
	import flash.utils.setTimeout;
	
	import main.App;
	import main.AppConfig;
	import main.AppNet;
	import main.mvc.GModelData;
	
	import module.building.model.dataObj.BuildingObj;
	import module.edittroop.cmds.EditTroopCmdConst;
	import module.edittroop.model.dataObj.ArmsConfigObj;
	import module.edittroop.model.dataObj.HeroConfigObj;
	import module.scene.cmds.SceneCommand;
	import module.scene.cmds.SceneModelCommand;
	import module.scene.model.dataObj.IsoDataObj;
	import module.scene.model.dataObj.TerritoryIsoDataObj;
	import module.scene.model.dataObj.TroopIsoDataObj;
	
	/**
	 *	场景数据层 
	 * @author Roland
	 * 
	 */
	public class SceneModelData extends GModelData
	{
		//==================后台数据通信命令===============
		/**
		 *	 发送部队移动信息
		 */
		private static const SEND_TROOP_MOVE:String = "troop_move";
		
		/**
		 *	接收到服务器的部队移动通知
		 */
		private static const GET_TROOP_MOVE:String = "troop_move";

		/**
		 *	接收部队列表刷新通知 
		 */
		private static const GET_TROOP_UPDATE:String = "troop_update";
		
		/**
		 *	用户信息 
		 */
		private static const GET_USER_INFO:String = "user_info";
		
		/**
		 *	全地图建筑列表刷新
		 */
		private static const GET_MAP_BUILDING:String = "map_building";
		
		/**
		 *	 摆放建筑信息推送
		 */
		private static const GET_ADD_BUILDING_INFO:String = "building_info";
		
		/**
		 *	删除建筑信息推送 
		 */
		private static const GET_REMOVE_BUILDING_INFO:String = "remove_building_info";
		
		/**
		 *	移动建筑信息推送 
		 */
		private static const GET_MOVE_BUILDING_INFO:String = "move_building_info";
		
		/**
		 *	服务器时间更新 
		 */
		private static const GET_SERVER_TIME:String = "server_time";
		//===================命令监听列表=================
		/**
		 *	服务器时间通知 
		 */
		private static var CMDS_SERVER_TIME_NOTICE:CmdNameList = new CmdNameList(SceneModelData.GET_SERVER_TIME);
		/**
		 *	部队移动信息命令名列表 
		 */
		private static var CMDS_TROOP_MOVE_NOTICE:CmdNameList = new CmdNameList(SceneModelData.GET_TROOP_MOVE);
		/**
		 *	部队列表更新信息命令名列表 
		 */
		private static var CMDS_TROOP_UPDATE_NOTICE:CmdNameList = new CmdNameList(SceneModelData.GET_TROOP_UPDATE);

		/**
		 *	用户信息命令名列表 
		 */
		private static var CMDS_USER_INFO_NOTICE:CmdNameList = new CmdNameList(SceneModelData.GET_USER_INFO);
		
		/**
		 *	 全地图建筑动态信息命令名列表
		 */
		private static var CMDS_MAP_BUILDING_NOTICE:CmdNameList = new CmdNameList(SceneModelData.GET_MAP_BUILDING);
		
		/**
		 *	摆放建筑信息推送 命令名列表
		 */
		private static var CMDS_ADD_BUILDING_INFO_NOTICE:CmdNameList = new CmdNameList(SceneModelData.GET_ADD_BUILDING_INFO);
		
		/**
		 *	移除建筑推送 命令名列表 
		 */
		private static var CMDS_REMOVE_BUILDING_INFO_NOTICE:CmdNameList = new CmdNameList(SceneModelData.GET_REMOVE_BUILDING_INFO);
		
		/**
		 *	移动建筑推送 命令名列表 
		 */
		private static var CMDS_MOVE_BUILDING_INFO_NOTICE:CmdNameList = new CmdNameList(SceneModelData.GET_MOVE_BUILDING_INFO);
		private var _objectConfigDic:Dictionary;
		private var _objectConfigList:Array;
		
		private var _heroConfigDic:Dictionary;
		private var _armsConfigDic:Dictionary;
		private var _buildingConfigDic:Dictionary;
		/**
		 * 用户领地信息 
		 */
		public var userTerritoryObj:TerritoryIsoDataObj;
		public function SceneModelData()
		{
			super();
			init();
		}
		
		private function init():void
		{
			initConfigData();
			initRegister();
		}
		
		private function initRegister():void
		{
			register(CMDS_SERVER_TIME_NOTICE,getServerTimeHandler);
			register(CMDS_TROOP_MOVE_NOTICE,getTroopMoveInfoHandler);
			register(CMDS_TROOP_UPDATE_NOTICE,getTroopUpdateInfoHandler);
			register(CMDS_USER_INFO_NOTICE,getUserInfoHandler);
			register(CMDS_MAP_BUILDING_NOTICE,getMapBuildingInfoHandler);
			register(CMDS_ADD_BUILDING_INFO_NOTICE,getAddBuildingInfoHandler);
			register(CMDS_REMOVE_BUILDING_INFO_NOTICE,getRemoveBuildingInfoHandler);
			register(CMDS_MOVE_BUILDING_INFO_NOTICE,getMoveBuildingInfoHandler);
			
			//锁定建筑相关信息，直到获取到领地信息后再处理//TODO等待锁定解锁命令写完，启用下方语句控制建筑加载时机
//			lockCmdList(CMDS_MAP_BUILDING_NOTICE);
//			lockCmdList(CMDS_ADD_BUILDING_INFO_NOTICE);
//			lockCmdList(CMDS_REMOVE_BUILDING_INFO_NOTICE);
//			lockCmdList(CMDS_MOVE_BUILDING_INFO_NOTICE);
		}

		
		private function initConfigData():void
		{
			//场景基本物件配置
			_objectConfigDic = new Dictionary();
			_objectConfigList = [];
			var nameList:Array = [ConfigConst.BUILDING,ConfigConst.FLOOR,ConfigConst.TROOP,ConfigConst.DECORATION,ConfigConst.COLLECTION];
			for (var i:int = 0; i < nameList.length; i++) 
			{
				var list:Array = this.getDataBySource(nameList[i] + "_org") as Array;
				if(list)
				{
					for (var j:int = 0; j < list.length; j++) 
					{
						var obj:IsoDataObj = IsoDataObj.parseData(list[j]);
						_objectConfigDic[obj.serverId] = obj;
						_objectConfigList.push(obj);
					}
				}
			}
			
			//英雄配置
			_heroConfigDic = new Dictionary();
			var heroList:Array = this.getDataBySource(ConfigConst.HERO) as Array;
			for (var k:int = 0; k < heroList.length; k++) 
			{
				var heroObj:HeroConfigObj = heroList[k];
				if(heroObj)
				{
					_heroConfigDic[heroObj.serverId] =heroObj;
				}
			}
			
			//士兵配置
			_armsConfigDic = new Dictionary();
			var armsList:Array = this.getDataBySource(ConfigConst.ARMS) as Array;
			for (var m:int = 0; m < armsList.length; m++) 
			{
				var armsObj:ArmsConfigObj = armsList[m];
				if(armsObj)
				{
					_armsConfigDic[armsObj.serverId] =armsObj;
				}
			}
			
			//建筑配置
			_buildingConfigDic = new Dictionary();
			var buildingList:Array = this.getDataBySource(ConfigConst.BUILDING) as Array;
			for (var n:int = 0; n < buildingList.length; n++) 
			{
				var buildingObj:BuildingObj = buildingList[n];
				if(buildingObj)
				{
					_buildingConfigDic[buildingObj.serverId] = buildingObj;
				}
			}
			
		}
		
		/**
		 *  发送部队移动命令
		 * @param troopId
		 * 
		 */
		public function sendTroopMoveInfo(troopId:String,path:Array):void
		{
			var info:Object = {};
			info.troop_id = troopId;
			info.pos_array = path;
			var data:Object = {"info":info}
			AppNet.getInstance().send(SEND_TROOP_MOVE,data);
			trace("send:"+path);
			//测试代码
//			setTimeout(getTroopMoveInfoHandler,100,info);
		}
		
		private function getServerTimeHandler(data:Object):void
		{
			var server_time:uint = data.server_time;
			if(server_time != 0)
			{
				App.stime = uint(data);
			}
		}
		
		private function getTroopMoveInfoHandler(data:Object):void
		{
//			trace(JSON.encode(data));
			var nt:Object = data[GET_TROOP_MOVE];
			if(nt)
			{
				var cmd:SceneModelCommand = new SceneModelCommand(SceneModelCommand.TROOP_PATH_CHANGED);
				var info:Object = {};
				info.troopId = nt.troop_id;
				info.path = nt.pos_array;
				cmd.data = info;
				dispatch(cmd);
			}
		}
		
		
		private function getTroopUpdateInfoHandler(data:Object):void
		{
			var nt:Object = data[GET_TROOP_UPDATE];
			if(nt)
			{
				for(var itemId:String in nt)
				{
					var troopInfo:Object = nt[itemId];
					var troops:Array = troopInfo.troops;
					var userId:String = troopInfo.uid;
					for (var i:int = 0; i < troops.length; i++) 
					{
						var singleTroop:Object = troops[i];
						var obj:TroopIsoDataObj = new TroopIsoDataObj();
						obj.size = 2;
						obj.walkable = 1;
						obj.isoType = IsoType.OBJECT;
						obj.userId = userId;
						obj.troopId = singleTroop.troop_id ? singleTroop.troop_id : singleTroop.tid;
						obj.troopName = singleTroop.troop_name;
						obj.troopAttack = singleTroop.attack;
						obj.troopDefens = singleTroop.defens;
						obj.troopSkills = singleTroop.skills;
						obj.absX = singleTroop.x;
						obj.absZ = singleTroop.y;
						obj.troopNum = singleTroop.num;
						obj.speed = singleTroop.speed;
						obj.heroId = singleTroop.hero_id;
						obj.armsId = singleTroop.arms_id;
						var $cmd:Command = new Command(EditTroopCmdConst.EDIT_NEW_TROOP_SUCCESS);
						$cmd.data = obj;
						dispatch($cmd);
					}
				}
			}
		}
		
		
		private function getUserInfoHandler(data:Object):void
		{
			if(userTerritoryObj !=null)return;
			var $dataObj:IsoDataObj = getIsoObjById("5000");
			var $obj:TerritoryIsoDataObj = new TerritoryIsoDataObj();
			$obj.serverId = $dataObj.serverId;
			$obj.direction = $dataObj.direction;
			$obj.size = $dataObj.size;
			$obj.walkable = $dataObj.walkable;
			$obj.isoType = $dataObj.isoType;
			$obj.parseDynamicData(data);
			userTerritoryObj = $obj;
			var $cmd:SceneModelCommand = new SceneModelCommand(SceneModelCommand.GET_TERRITORY_POSITION);
			$cmd.data = $obj;
			dispatch($cmd);
			
			//获取用户领地信息后解锁建筑信息锁定，添加建筑 //TODO等待锁定解锁命令写完，启用下方语句控制建筑加载时机
//			unlockCmdList(CMDS_MAP_BUILDING_NOTICE);
//			unlockCmdList(CMDS_ADD_BUILDING_INFO_NOTICE);
//			unlockCmdList(CMDS_REMOVE_BUILDING_INFO_NOTICE);
//			unlockCmdList(CMDS_MOVE_BUILDING_INFO_NOTICE);
		}
		
		
		private function getMoveBuildingInfoHandler(data:Object):void
		{
			var $buildingArr:Array = data.move_building_info as Array;
			var $buildingList:Array = [];
			
			for (var i:int = 0; i < $buildingArr.length; i++) 
			{
				var objData:Object = parseBuildingData($buildingArr[i]);
				if(objData)
				{
					$buildingList.push(objData);
				}
			}
			var $cmd:SceneModelCommand = new SceneModelCommand(SceneModelCommand.MOVE_BUILDING_LIST_UPDATE);
			$cmd.data = $buildingList;
			dispatch($cmd);
		}
		
		
		private function getRemoveBuildingInfoHandler(data:Object):void
		{
			var $buildingArr:Array = data.remove_building_info as Array;
			var $buildingList:Array = [];
			
			for (var i:int = 0; i < $buildingArr.length; i++) 
			{
				var objData:Object = parseBuildingData($buildingArr[i]);
				if(objData)
				{
					$buildingList.push(objData);
				}
			}
			var $cmd:SceneModelCommand = new SceneModelCommand(SceneModelCommand.REMOVE_BUILDING_LIST_UPDATE);
			$cmd.data = $buildingList;
			dispatch($cmd);
		}
		
		private function getAddBuildingInfoHandler(data:Object):void
		{
			var $buildingArr:Array = data.building_info as Array;
			var $buildingList:Array = [];

			for (var i:int = 0; i < $buildingArr.length; i++) 
			{
				var objData:Object = parseBuildingData($buildingArr[i]);
				if(objData)
				{
					$buildingList.push(objData);
				}
			}
			var $cmd:SceneModelCommand = new SceneModelCommand(SceneModelCommand.ADD_BUILDING_LIST_UPDATE);
			$cmd.data = $buildingList;
			dispatch($cmd);
		}
		
		private function getMapBuildingInfoHandler(data:Object):void
		{
			var $mapBuilding:Object = data.map_building;
			var $buildingList:Array = [];
			for each(var buildingData:Object in $mapBuilding) 
			{
				var objData:Object = parseBuildingData(buildingData);
				if(objData)
				{
					$buildingList.push(objData);
				}
			}
			var $cmd:SceneModelCommand = new SceneModelCommand(SceneModelCommand.MAP_BUILDING_LIST_UPDATE);
			$cmd.data = $buildingList;
			dispatch($cmd);
			
		}
		
		/**
		 *	填充单个后台建筑数据  
		 * @param data {"x":301454,"completed_time":1322731602,"y":301769,"height":2,"status":1,"level":1,"eid":1322731422019,"width":2,"type":"15301","uid":989}
		 * @return {"dataObj":dataObj,"isoDataObj":isoDataObj}
		 * 
		 */
		private function parseBuildingData(buildingData:Object):Object
		{
			var serverId:String = buildingData.type;
			var dataObj:BuildingObj = _buildingConfigDic[serverId] ? (_buildingConfigDic[serverId] as BuildingObj).clone() : null;
			var isoDataObj:IsoDataObj =_objectConfigDic[serverId] ? (_objectConfigDic[serverId] as IsoDataObj).clone() : null;
			if(dataObj && isoDataObj)
			{
				isoDataObj.eId = buildingData.eid;
				isoDataObj.userId = buildingData.uid;
				isoDataObj.absX = buildingData.x;
				isoDataObj.absZ = buildingData.y;
				if(buildingData.hasOwnProperty("open_status")){
					isoDataObj.walkable = buildingData.open_status;
				}
				dataObj.level = buildingData.level;
				dataObj.completeTime = buildingData.completed_time;
				dataObj.status =  buildingData.status;
				dataObj.currentOrderArr = buildingData.cur_order_arr;
				return {"dataObj":dataObj,"isoDataObj":isoDataObj};
			}
			return null;
		}
		
		/**
		 *	根据serverId获取物件配置信息强类型 
		 * @param serverId
		 * @return 
		 * 
		 */
		public function getIsoObjById(serverId:String):IsoDataObj
		{
			return _objectConfigDic[serverId] as IsoDataObj;
		}
		
		/**
		 *	获取所有场景物件的配置数据列表 
		 * @return 
		 * 
		 */
		private function get isoObjConfigList():Array
		{
			return _objectConfigList;
		}
	}
}